package com.hnzq.cloud.freshaddressassembly.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hnzq.cloud.api.SiteAreaApi;
import com.hnzq.cloud.api.WarehouseApi;
import com.hnzq.cloud.dto.ListSiteAreaDto;
import com.hnzq.cloud.dto.OneSiteAreaDto;
import com.hnzq.cloud.freshaddressassembly.domain.FreSiteArea;
import com.hnzq.cloud.freshaddressassembly.domain.FreWarerhouse;
import com.hnzq.cloud.freshaddressassembly.service.IFreWarerhouseService;
import com.hnzq.cloud.freshaddressassembly.service.impl.FreSiteAreaServiceImpl;
import com.hnzq.cloud.freshaddressassembly.util.WoniuBeanUtils;
import com.hnzq.cloud.model.Result;
import com.hnzq.cloud.warehouseParam.AddParam;
import com.hnzq.cloud.warehouseParam.FindAroundSiteAreaParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;


/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author qinle
 * @since 2022-02-10
 */
@RestController
@RequestMapping("/freWarerhouse")
public class FreWarerhouseController implements WarehouseApi {
    @Resource
    private IFreWarerhouseService freWarerhouseService;

    //新增一个仓库
    @PostMapping("/addWarehouse")
    @Override
    public Result addWarehouse(AddParam addParam) {
        if (freWarerhouseService.save(WoniuBeanUtils.copyObject(addParam, FreWarerhouse.class))) {
            return Result.newInstance().setMessage("新增成功");
        }else {
            return Result.newInstance().setMessage("新增失败");
        }
    }
    //删除一个仓库
    @DeleteMapping("/deletWarehouse")
    @Override
    public Result deletWarehouse(Integer warehouseId) {
        FreWarerhouse freWarerhouse = freWarerhouseService.getById(warehouseId);
        freWarerhouse.setIsDelete(1);
        if (freWarerhouseService.saveOrUpdate(freWarerhouse)) {
            return Result.newInstance().setMessage("删除成功");
        }else {
            return Result.newInstance().setMessage("删除失败");
        }
    }
    //修改仓库信息，
    @PutMapping("/updateWarehouse")
    @Override
    public Result updateWarehouse(AddParam addParam) {
        FreWarerhouse freWarerhouse = freWarerhouseService.getOne(new QueryWrapper<FreWarerhouse>().eq("user_id", addParam.getUserId()));
        FreWarerhouse newferWarehouse = new FreWarerhouse();
        FreWarerhouse warerhouse = WoniuBeanUtils.copyObject(addParam, newferWarehouse.getClass());
        warerhouse.setWarehouseId(freWarerhouse.getWarehouseId());
        if (freWarerhouseService.saveOrUpdate(warerhouse)) {
            return Result.newInstance().setMessage("修改成功");
        }else {
            return Result.newInstance().setMessage("修改失败");
        }
    }
    //查询一个仓库信息
    @GetMapping("/findWarehouse")
    @Override
    public Result<FreWarerhouse> findWarehouse(Integer warehouseId) {
        return  Result.newInstance().setData(freWarerhouseService.getById(warehouseId));
    }
    @Resource
    private FreSiteAreaServiceImpl freSiteAreaService;
    //根据仓库信息查询所有的站点区域
    @GetMapping("/findAroundSiteArea")
    @Override
    public Result<ListSiteAreaDto> findAroundSiteArea(FindAroundSiteAreaParam findAroundSiteAreaParam) {
        List<FreSiteArea> listSiteArea = freSiteAreaService.list(new QueryWrapper<FreSiteArea>().eq("is_delete", 0));
        System.out.println(listSiteArea);
        ListSiteAreaDto listSiteAreaDto = new ListSiteAreaDto();
        List<OneSiteAreaDto>list=new ArrayList<>();
        for (FreSiteArea freSiteArea : listSiteArea) {
            if (WoniuBeanUtils.getNewDistance(Double.valueOf(findAroundSiteAreaParam.getWarehouseX()),Double.valueOf(findAroundSiteAreaParam.getWarehouseY()),Double.valueOf(freSiteArea.getAreaX()),Double.valueOf(freSiteArea.getAreaY()))<20000.0){
                list.add(WoniuBeanUtils.copyObject(freSiteArea,OneSiteAreaDto.class));
            }
        }
        listSiteAreaDto.setListSiteArea(list);
        return Result.newInstance().setMessage("查询"+findAroundSiteAreaParam.getWarehouseName()+"仓库所有站点区域成功").setData(listSiteAreaDto);
    }






    //根据站点区域标签（主键）查找订单
//    @GetMapping("/findOder")
//    @Override
//    public Result findOder(Integer siteAreaId) {
//        Result result = siteQueryApi.findAllSite(siteAreaId);
//        Object data = result.getData();
//        //将同一个区域下所有门店去取出来
//        List<FreSite> freSiteList = WoniuBeanUtils.objToList(data);
//        ArrayList<Integer> listSiteIds = new ArrayList<>();
//        //将门店id取出来放入集合中
//        for (int i=0;i<=freSiteList.size();i++){
//            listSiteIds.add(freSiteList.get(i).getSiteId());
//        }
//        //根据门店id去查询订单
//        //调用oder接口
//
//        return null;
//    }
}

